Skip to content

fix: Faker::Config.lazy_loading configuration must be respected (WIP)#3256

Draft
thdaraujo wants to merge 2 commits intomainfrom
ta/fix-lazy-loading-bug
Draft

fix: Faker::Config.lazy_loading configuration must be respected (WIP)#3256
thdaraujo wants to merge 2 commits intomainfrom
ta/fix-lazy-loading-bug

Conversation

@thdaraujo
Copy link
Copy Markdown
Contributor

(fixes #3248 )

(This is work in progress)

This fix initializes lazy loading or eager loading after the first time a generator is called, which can be a bit surprising.

But this guarantees that the Faker::Config.lazy_loading setting is respected.

Motivation / Background

This Pull Request has been created because [REPLACE ME].

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug, refactor something, or add a feature.
  • Tests and Rubocop are passing before submitting your proposed changes.

If you're proposing a new generator or locale:

  • Double-check the existing generators documentation to make sure the new generator you want to add doesn't already exist.
  • You've reviewed and followed the Contributing guidelines.

This fix initializes lazy loading or eager loading
after the first time a generator is called, which can
be a bit surprising.

But this guarantees that the `Faker::Config.lazy_loading`
setting is respected.
@thdaraujo thdaraujo self-assigned this May 1, 2026
Comment thread lib/faker.rb
end.join('/')
end

# TODO: refactor this
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can move this out to a lazy loading class or module

@thdaraujo
Copy link
Copy Markdown
Contributor Author

testing script

require_relative "lib/faker"

lazy_loading = if ENV.key?('FAKER_LAZY_LOAD')
                 puts 'set via env...'
                 ENV['FAKER_LAZY_LOAD'] == '1'
               else
                 puts 'set via config...'
                 Faker::Config.lazy_loading = false # true
               end

puts "Number of constants: #{Faker.constants.size}"
puts "Faker::Name #{Faker::Name.name}"
puts "Number of constants: #{Faker.constants.size}"

if lazy_loading
  puts "faker is lazy loading..."

  if Faker.constants.size > 10
    raise "must be lazy loaded!"
  else
    "lazy loading success!"
  end
else
  puts "faker is NOT lazy loading..."

  if Faker.constants.size < 10
    raise "must be eager loaded!"
  else
    "eager loading success!"
  end
end

@thdaraujo thdaraujo force-pushed the ta/fix-lazy-loading-bug branch from 810d21a to 336ca2e Compare May 1, 2026 03:40
@thdaraujo thdaraujo force-pushed the ta/fix-lazy-loading-bug branch from f6c2113 to 471afd9 Compare May 1, 2026 03:42
Comment thread lib/faker.rb
private_constant :EAGER_LOAD_MUTEX

# initial usage determines lazy loading or eager loading
# TODO: this can be a bit surprising and error-prone
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sets up loading the first time a generator is called, which could be a bit surprising/unexpected.

maybe it's a good enough solution for letting folks test it?

in the future, we aim to make lazy loading default, so this wouldn't be a problem anymore, in my view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The setting Faker::Config.lazy_loading = true does not work

1 participant